import {
  getAccountList,
  resetAccountPassword,
  changeOperationAccountStatus,
  addAccount,
  updateAccount,
  getAccountInfo
} from '@/api/authManage'
import { cloneDeep } from 'lodash'
const authManage = {
  namespaced: true,
  state: {

  },
  mutations: {
    // SET_TOKEN: (state, token) => {

    // }
  },

  actions: {
    // 获取账户列表
    getAccountList({ commit, state }, payload) {
      const ERR_MSG = '账户列表获取失败'
      return new Promise((resolve, reject) => {
        getAccountList(payload).then(Response => {
          var {
            code = '',
            data = {},
            msg = ERR_MSG
          } = Response.data || {}
          if (code.toString() === '200') {
            resolve(cloneDeep(data))
          } else {
            reject(msg || ERR_MSG)
          }
          resolve(Response)
        }).catch(() => {
          reject(ERR_MSG)
        })
      })
    },
    // 重置密码
    resetAccountPassword({ commit, state }, payload) {
      const ERR_MSG = '重置密码失败，请重试！'
      return new Promise((resolve, reject) => {
        resetAccountPassword(payload).then(Response => {
          var {
            code = '',
            data = {},
            msg = ERR_MSG
          } = Response.data || {}
          if (code.toString() === '200') {
            resolve(cloneDeep(data))
          } else {
            reject(msg || ERR_MSG)
          }
          resolve(Response)
        }).catch(() => {
          reject(ERR_MSG)
        })
      })
    },
    // 启停账户
    changeOperationAccountStatus({ commit, state }, payload) {
      const ERR_MSG = '操作失败请重试！'
      return new Promise((resolve, reject) => {
        changeOperationAccountStatus(payload).then(Response => {
          var {
            code = '',
            data = {},
            msg = ERR_MSG
          } = Response.data || {}
          if (code.toString() === '200') {
            resolve(cloneDeep(data))
          } else {
            reject(msg || ERR_MSG)
          }
          resolve(Response)
        }).catch(() => {
          reject(ERR_MSG)
        })
      })
    },
    // 新增账户
    addAccount({ commit, state }, payload) {
      const ERR_MSG = '新建账户失败'
      return new Promise((resolve, reject) => {
        addAccount(payload).then(Response => {
          var {
            code = '',
            data = {},
            msg = ERR_MSG
          } = Response.data || {}
          if (code.toString() === '200') {
            resolve(cloneDeep(data))
          } else {
            reject(msg || ERR_MSG)
          }
          resolve(Response)
        }).catch(() => {
          reject(ERR_MSG)
        })
      })
    },
    // 修改账户
    updateAccount({ commit, state }, payload) {
      const ERR_MSG = '修改账户失败'
      return new Promise((resolve, reject) => {
        updateAccount(payload).then(Response => {
          var {
            code = '',
            data = {},
            msg = ERR_MSG
          } = Response.data || {}
          if (code.toString() === '200') {
            resolve(cloneDeep(data))
          } else {
            reject(msg || ERR_MSG)
          }
          resolve(Response)
        }).catch(() => {
          reject(ERR_MSG)
        })
      })
    },
    // 账户详情
    getAccountInfo({ commit, state }, payload) {
      const ERR_MSG = '获取账户详情失败'
      return new Promise((resolve, reject) => {
        getAccountInfo(payload).then(Response => {
          var {
            code = '',
            data = {},
            msg = ERR_MSG
          } = Response.data || {}
          if (code.toString() === '200') {
            resolve(cloneDeep(data))
          } else {
            reject(msg || ERR_MSG)
          }
          resolve(Response)
        }).catch(() => {
          reject(ERR_MSG)
        })
      })
    }
  }
}

export default authManage
